<?php

$FileList = IncomeTransact_LoadFile($GETCOMMETH[1]['tcmtChannel'], $GetSettings[1]['set_Valor'], $CORRESP_FTDIR, $PID, $PrefixIndex[$PID]);

for ($pI = 0; $pI < count($FileList); $pI++) {

    $TransmissionResultCode = 0;
    $handle = fopen($GetSettings[1]['set_Valor'] . "/" . $CORRESP_FTDIR . "/IN/" . $FileList[$pI], "r");

    #Pre-set Vars
    $SUMREFA = 0;
    $SUMSENT = 0;

    $BankCheck = '';
    CONN('MYT');
    $FILESEP[] = array('', ',', ';', '	');
    $fileprp[] = substr($FileList[$pI], 0, 3);
    $row = 1;

    #	Valida
    $CorrProperties = $GLOBALS['MYT']->db_array("SELECT CorrNumero, CorrFormatoFichero, CorrAlias, CorrAgente, EXT, DELIMITER, DELIMITER, OB, VERSION, LENGTH, METRIC, ffFilenameFormat FROM corresponsales
													LEFT OUTER JOIN hd_fileformats ON REF = CorrFormatoFichero WHERE CorrAgente = '{$GETCOMMETH[1]['CorrAgente']}'");

    if (!isset($CorrProperties[1])) {
        #Gerar alerta, Recebida transaccao de correspondente inexiste ou com configuracao incorrecta
        echo "Invalid request";
    } else {
        if ($CorrProperties[1]['CorrFormatoFichero'] == 7) {

        }

        if (isset($orOrder[1])) {
            $Msg_ERROR = 'Lote ja existe.';
        }
        if ($fileprp[0] != $CorrProperties[1]['CorrAgente']) {
            $Msg_ERROR = 'O ficheiro nao e valido.';
        }

        $ReceivedDate = substr($sysTime, 0, 10);
        $ReceivedTime = substr($sysTime, 11, 18);

        if ($CorrProperties[1]['EXT'] != 'xml') {
            while (($data = fgetcsv($handle, 2048, ';')) !== FALSE) {
                include($GLOBALS['BIB'] . 'FileTemplates/TransactModels/' . $CorrProperties[1]['DELIMITER'] . '_' . $CorrProperties[1]['EXT'] . '_' . $CorrProperties[1]['OB'] . '_' . $CorrProperties[1]['VERSION'] . '_' . $PID . '.php');
            }
        } else {
            echo $data_contents = xml_getparse(file_get_contents($GetSettings[1]['set_Valor'] . "/" . $CORRESP_FTDIR . "/IN/" . $FileList[$pI], true));
            $data_count = count($data_contents);
            while ($row <= $data_count) {
                $data = $data_contents['Incident'];
                include($GLOBALS['BIB'] . 'FileTemplates/TransactModels/' . $CorrProperties[1]['DELIMITER'] . '_' . $CorrProperties[1]['EXT'] . '_' . $CorrProperties[1]['OB'] . '_' . $CorrProperties[1]['VERSION'] . '_' . $PID . '.php');
            }
        }
        fclose($handle);
        #	Regista Lote
        $CInvoice = $row - 1;
        $MDP = CONN('DBP');
        $MDP->multi_query("CALL fn_incidence (@IncidentID,{$FraID},'{$sysTime}',{$_SESSION[$SESSID]},'{$data['Notes']}',2,
               '{$data['FieldName']}','{$data['FieldOldValue']}','{$data['FieldNewValue']}',1,@IncidentID); SELECT @IncidentID");
        do {
            if ($result = $MDP->store_result()) {
                while ($row = $result->fetch_row()) {
                    $Msg_CONF = 1;
                }
                $result->free();
            }
        } while ($MDP->next_result());
        $MDP->close();
    }
    IncomeTransact_StoreFile($TransmissionResultCode, $GetSettings[1]['set_Valor'], $CORRESP_FTDIR, $FileList[$pI], $GLOBALS['DOC']);
}
?>